
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <vector>
#include <share.h>

using namespace std;

int inkey()
{
	if (_kbhit()) {
		return getch();
	}
	else {
		return 0;
	}
}

int main(int argc, char **argv)
{
	const int MAX_LENGTH = 1024;
	vector<FILE *> streams;
	char line[MAX_LENGTH];

	if (argc < 2) {
		printf("Usage: fread FILE1 [FILE2] [FILE3...]\n");
		return -1;
	}

	// open all files to 'read'
	for (int i = 1; i < argc; i++) {
		FILE *f = _fsopen(argv[i], "r", _SH_DENYNO);
		if (f == NULL) {
			printf("Failed open file: %s\n", argv[i]);
		}
		else {
			printf("Add file to read: %s\n", argv[i]);
			streams.push_back(f);
		}
	}

	// read content
	printf("Press 'q' to quit...\n");
	while (inkey() !=  'q') {
		for (int i = 0; i < streams.size(); i++) {
			memset(line, 0, sizeof(line));
			while (fgets(line, MAX_LENGTH - 1, streams[i]) != NULL) {
				printf("[%s]: %s", argv[i+1], line);
			}
		}
		Sleep(1);
	}

	// close all opened files
	for (int i = 0; i < streams.size(); i++) {
		fclose(streams[i]);
	}

	return 0;
}
